<?php
/**
 * To use MiniMessages you should look at GadgetsMiniMessage.class. You can find OpenSocial documentation here [http://code.google.com/apis/opensocial/docs/0.7/reference/gadgets.MiniMessage.html Class gadgets.MiniMessage]. This allows you to send mini messages to the user that is using the application. You can create messages, remove them pragmatically or you can create dismissible messages.
 * 
 * Here I'm going to explain how you could use MiniMessages in the sfOpenSocialPlugin. 
 * You can choose to use this class in the view or in the action. In this example I'm going to create a dismissible MiniMessage Object in the action and render it in the view. You can find the complete code under "sfOpenSocialPlugin/modules/application"
 * <code>
 * //action
 * $dismissibleMessage = new GadgetsMiniMessage('dismissibleMessage');
 * $this->dismissibleMessageFunction = new JSFunction('dismissibleMessageFunction');
 * 
 * $content = $dismissibleMessage->initialize();
 * $content .= $dismissibleMessage->createDismissibleMessage('<div>You can dismiss this message!</div>');
 * $this->dismissibleMessageFunction->setContent($content);
 * echo JSFunction::addJSTags($this->dismissibleMessageFunction);
 * </code>
 * Here I've created an GadgetsMiniMessage object and also an object representing an JavaScript function. This was made, because I wanted to do all the logic in the action.
 * The first thing to do when using GadgetsMiniMessage class is to initialize the object. If you don't do this, you will receive an sfException when using any other method. The $content variable is used to gather the content for the javascript function, but if we wanted to do this part in the view, just doing "echo" it would suffice.
 * To make use of this messages, I've created the following view:
 * <code>
 * <?php use_helper('OpenSocial'); ?>
 * 
 * <div id="MiniMessage"></div>
 * <?php echo add_jsfunction_anchor('Create Dismissible Message',$dismissibleMessageFunction) ?>
 * <?php echo add_jsfunction_anchor('Create Static Message',$staticMessageFunction) ?>
 * <?php echo add_jsfunction_anchor('Create Timer Message',$timerMessageFunction) ?>
 * <?php echo add_jsfunction_anchor('Dismiss Static Timer Message',$dismissStaticMessageFunction) ?>
 * </code>
 * In the view I've created an anchor any of the JavaScript functions that were created. This code will also include the "<Require feature="minimessage"/>" in the gadget
 * 
 * @package    gadgets
 * @subpackage feature
 * @author     Daniel Botelho <botelho.daniel@gmail.com>
 */
class GadgetsMiniMessage implements Instanciatable
{
  private $var;
  private $initizalized;
  private $opt_moduleId;
  private $opt_container;
  
  public function __construct($var,$opt_moduleId=null, $opt_container=null)
  {
    $this->var = $var;
    $this->opt_container = $opt_container;
    $this->opt_moduleId = $opt_moduleId;
    $this->initizalized = false;
  }
  
  public function initialized()
  {
  	return $this->initizalized;
  }
  
  public function initialize($initialize=true,$appendable=false)
  {
  	$html = '';
  	
    if($initialize)
  	  $html = 'var '. $this->var." = ";
  	  
  	if(!$this->opt_container && !$this->opt_moduleId)
  	  $html .= "new gadgets.MiniMessage()";
  	else {
  	  $container = ($this->opt_container)? $this->opt_container : 'null';
  	  $moduleId = ($this->opt_moduleId)? $this->opt_moduleId : 'null';
  	  $html .= "new gadgets.MiniMessage($moduleId,$container)";
  	}
  	
  	$this->initizalized = true;
    OSConfig::importMiniMessage();
    
  	return StringUtil::appendableString($html,$appendable);
  }
  
  /**
   * This method returns the variable name of this MiniMessage class
   * @return string The variable used for this MiniMessage
   */
  public function getVarName(){ return $this->var; }
  
  /**
   * Creates a dismissible message with an [x] icon that allows users to dismiss the message. When the message is dismissed, it is removed from the DOM and the optional callback function, if defined, is called.
   *
   * @param string $message The message as an HTML string or DOM element
   * @param JSFunction $opt_callback Optional callback function to be called when the message is dismissed
   * @param boolean $appendable
   * @return string HTML element of the created message
   */
  public function createDismissibleMessage($message,$opt_callback=null,$appendable=false)
  {
  	if(!$this->initialized())
  	  throw new sfException('Must call method initialize before use this method');

  	$html = '';
    $html .= $this->getVarName().".createDismissibleMessage($message";

    if($opt_callback)
  	  $html .= ",".$opt_callback->getName();
  	
  	return StringUtil::appendableString($html.')',$appendable); 
  }
  
  /**
   * Creates a static message that can only be dismissed programmatically (by calling dismissMessage()).
   *
   * @param string $message The message as an HTML string or DOM element
   * @param boolean $appendable
   * @return string HTML element of the created message
   */
  public function createStaticMessage($message,$element = null, $appendable=false)
  {
  	if(!$this->initialized())
  	  throw new sfException('Must call method initialize before use this method');

  	$html = '';
  	if($element)
  	{
  	  $div = $element."_div";
      $html .= "var $div = document.createElement(\"$element\");\n";
      $html .= "$div.innerHTML = '$message';\n";
      $html .= $this->getVarName().".createStaticMessage($div)";
  	}
  	else
  	{
  	  $html .= $this->getVarName().".createStaticMessage('$message')";
  	}
  	
  	return StringUtil::appendableString($html,$appendable); 
  }
  
  /**
   * Creates a message that displays for the specified number of seconds. When the timer expires, the message is dismissed and the optional callback function is executed.
   *
   * @param string $message The message as an HTML string or DOM element
   * @param int $seconds Number of seconds to wait before dismissing the message
   * @param JSFunction $opt_callback Optional callback function to be called when the message is dismissed
   * @param boolean $appendable
   * @return string HTML element of the created message
   */
  public function createTimerMessage($message, $seconds, $opt_callback=null, $appendable=false)
  {
  	if(!$this->initialized())
  	  throw new sfException('Must call method initialize before use this method');

  	$html = '';
    $html .= $this->getVarName().".createTimerMessage($message,$seconds";
  	if($opt_callback)
  	  $html .= ",".$opt_callback->getName();
  	  
  	return StringUtil::appendableString($html.')',$appendable); 
  }
  
  /**
   * Dismisses the specified message.
   *
   * @param string $div Element message  - HTML element of the message to remove
   * @param boolean $appendable
   * @return string 
   */
  public function dismissMessage($div, $appendable=false)
  {
  	if(!$this->initialized())
  	  throw new sfException('Must call method initialize before use this method');
  	  
  	$html = '';
  	$div_var = $div."_div";
  	$html .= 'var '.$div_var." = document.getElementById(\"$div\");\n";
  	$html .= $this->getVarName().'.dismissMessage('.$div_var.')';
  	  
  	return StringUtil::appendableString($html,$appendable); 
  }
}